import { message } from 'antd';
import { getCompanyBasicInfo, updateCompanyInfo, getIndustry } from '@/services/companyInfo';
export default {
  namespace: 'companyBasicInfo',
  state: { company: {} },
  effects: {
    *getBasicInfo({ payload }, { call, put }) {
      const res = yield call(getCompanyBasicInfo, payload);
      console.log('公司基本信息:', res);
      if (res.code === 0 && res.data.length > 0) {
        console.log('获取公司基本信息：', res);
        yield put({
          type: 'setData',
          payload: {
            company: res.data[0],
          },
        });
      }
    },
    *getIndustryData({ payload }, { call, put }) {
      // console.log('提交参数：', payload);
      const res = yield call(getIndustry, payload);
      if (+res.code === 0 && res.data.length > 0) {
        // console.log('获取的所有的行业：', res);
        yield put({
          type: 'setData',
          payload: {
            industry: res.data[0],
          },
        });
      }
    },
    *submitInfo({ payload }, { call, put }) {
      try {
        const res = yield call(updateCompanyInfo, payload);
        console.log('提交结果：', res);
        if (+res.code === 0) {
          message.success(res.message);
        } else {
          message.error(res.message);
        }
      } catch (e) {
        console.log(e);
        message.error('保存失败，请稍后再试');
      }
    },
  },
  reducers: {
    setData(state, { payload }) {
      const newState = {
        ...state,
        ...payload,
      };
      return newState;
    },
  },
};
